//二维数组的单点修改+某一特定值的区间查询(创建多个树状数组即可)
#include <iostream>
using namespace std;
#define lowbit(x)(x & -x)
const int N = 310, M = 110;
int n, m;
int a[N][N];

struct BIT
{
    int s[N][N];
    void modify(int x, int y, int k)
    {
        for(int i = x; i <= n; i += lowbit(i))
            for(int j = y; j <= m; j += lowbit(j))
                s[i][j] += k;
    }   
    
    int query(int x, int y)
    {
        int sum = 0;
        for(int i = x; i; i -= lowbit(i))
            for(int j = y; j; j -= lowbit(j))
                sum += s[i][j];
        return sum;
    }
}s[M];

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            s[a[i][j]].modify(i, j, 1);
        }
    }
    int q; cin >> q;
    while(q--)
    {
        int op; cin >> op;
        if(op == 1)
        {
            int x, y, c; cin >> x >> y >> c;
            s[a[x][y]].modify(x, y, -1);
            s[c].modify(x, y, 1);
            a[x][y] = c;
        }
        else
        {
            int x1, y1, x2, y2, c; cin >> x1 >> x2 >> y1 >> y2 >> c;
            cout << s[c].query(x2, y2) - s[c].query(x1 - 1, y2) - s[c].query(x2, y1 - 1) + s[c].query(x1 - 1, y1 - 1) << endl;
        }
    }
    return 0;
}
